using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace TradingPlatform.MarketTiming
{
    public partial class MarketTimingGui : Form
    {
        private readonly Dictionary<string, TradingProgressBar> _timeBars;
        private readonly string _configFileName;
        public MarketTimingGui()
        {
            InitializeComponent();

            // Start the current time timer.

            _configFileName = "MarketTimingConfiguration.csv";
            _timeBars = new Dictionary<string, TradingProgressBar>();
            DisplayTimeBars(this, null);
            Resize+=new EventHandler(DisplayTimeBars);
        }

        private void DisplayTimeBars(object sender, EventArgs e)
        {
            panelTimeBars.Controls.Clear();
            int timeBarWidth = Width - 30;
            const int timeBarHeight = 25;

            // Get the market open infomation.
            using(StreamReader sr = new StreamReader(_configFileName))
            {
                string line = sr.ReadLine();
                int locationY = 0;
                while (!string.IsNullOrEmpty(line))
                {
                    string[] tempInfo = line.Split(new char[] {','});
                    string symbol = tempInfo[0];
                    if (textBoxFilter.Text.Trim() == string.Empty || symbol.ToLower().IndexOf(textBoxFilter.Text.ToLower()) >= 0)
                    {
                        string marketOpenInfo = tempInfo[1];
                        AddTimeBar(new Point(0, locationY), new Size(timeBarWidth, timeBarHeight), symbol, marketOpenInfo);
                        locationY += timeBarHeight;
                    }
                    line = sr.ReadLine();
                }
            }
        }

        private void AddTimeBar(Point location, Size size, string symbol, string marketOpenInfo)
        {
            if (!_timeBars.ContainsKey(symbol))
            {
                _timeBars.Add(symbol, new TradingProgressBar());
            }
            TradingProgressBar timeBar = _timeBars[symbol];
            timeBar.BackColor = SystemColors.AppWorkspace;
            timeBar.BorderStyle = BorderStyle.FixedSingle;
            timeBar.Location = location;
            timeBar.Size = size;
            panelTimeBars.Controls.Add(timeBar);
            timeBar.Config(symbol, 210, 210, marketOpenInfo, 1000);
        }

        private void buttonReload_Click(object sender, EventArgs e)
        {
            DisplayTimeBars(this, null);
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {
            Process.Start("notepad.exe", _configFileName);
        }

        private void timerCurrentTime_Tick(object sender, EventArgs e)
        {
            BeginInvoke(new EventHandler(DisplayCurrentTime));
        }

        private void DisplayCurrentTime(object sender, EventArgs e)
        {
            Text = "MarketTiming " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}